﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using System.ComponentModel;
using CardProduce.Card;
using PersonalHealth.Data.Model;

namespace PersonHealth.View
{
    /// <summary>
    /// Index.xaml 的交互逻辑
    /// </summary>
    public partial class Index : Page
    {
        BackgroundWorker worker1;
        UInt32 kh = 0;
        string password = "FFFFFFFFFFFF";
        public Index()
        {
            InitializeComponent();
            this.tbName.Focus();
            this.Loaded += new RoutedEventHandler(Index_Loaded);
            this.Unloaded += new RoutedEventHandler(Index_Unloaded);
        }

        void Index_Unloaded(object sender, RoutedEventArgs e)
        {
            if (worker1 != null && worker1.IsBusy)
            {
                worker1.CancelAsync();
            }
        }

        void Index_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                CardSimpleAPI.ConnectByPassword(password, password);
            }
            catch (Exception)
            {
                return;
            }
            worker1 = new BackgroundWorker();
            worker1.DoWork += (o1, e1) =>
            {
                while (true)
                {
                    while (CardSimpleAPI.RequestIDLE() != 0)
                    {
                        if (worker1.CancellationPending)
                        {
                            e1.Cancel = true;
                            return;
                        }
                        System.Threading.Thread.Sleep(500);
                    }
                    byte size = 0;
                    byte status = CardAPI.CascAnticoll(0, CardAPI.AntiCollision1, ref kh);
                    if (status != 0)
                        throw new CardException(CardAPI.GetErrorMsg(status));
                    status = CardAPI.CascSelect(CardAPI.AntiCollision1, kh, ref size);
                    if (status != 0)
                        throw new CardException(CardAPI.GetErrorMsg(status));
                    if (kh > 0)
                    {
                        break;
                    }
                }
            };
            worker1.RunWorkerCompleted += (o2, e2) =>
            {
                if (!e2.Cancelled)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        IUserService service = new UserService();
                        var user = service.Login("0002", "2222");
                        if (user == null)
                        {
                            MessageBox.Show("用户名或者密码错误！");
                            return;
                        }
                        MeasureUser measureUser = user as MeasureUser;
                        if (measureUser == null)
                        {
                            MessageBox.Show("用户类型不是客户！");
                            return;
                        }
                        AppData.User = measureUser;
                        //NavigationService.Navigate(new Uri("View/PersonPage.xaml", UriKind.RelativeOrAbsolute));
                        NavigationService.Content = new PersonPage();
                    }), null);
                }
            };
            worker1.RunWorkerAsync();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string serviceNo = "0002";
            string password = "111111";
            IUserService service = new UserService();
            var user = service.Login(serviceNo, password);
            if (user == null)
            {
                MessageBox.Show("用户名或者密码错误！");
                return;
            }
            MeasureUser doctor = user as MeasureUser;
            if (doctor == null)
            {
                MessageBox.Show("用户类型不是客户！");
                return;
            }
            AppData.User = doctor;
            //NavigationService.Navigate(new Uri("View/PersonPage.xaml", UriKind.RelativeOrAbsolute));
            NavigationService.Content = new PersonPage();
        }

        private void Login_click(object sender, RoutedEventArgs e)
        {
            IUserService service = new UserService();
            var user = service.Login(tbName.Text, tbPsd.Password);
            if (user == null)
            {
                MessageBox.Show("用户名或者密码错误！");
                return;
            }
            MeasureUser doctor = user as MeasureUser;
            if (doctor == null)
            {
                MessageBox.Show("用户类型不是客户！");
                return;
            }
            AppData.User = doctor;
            //NavigationService.Navigate(new Uri("View/PersonPage.xaml", UriKind.RelativeOrAbsolute));
            NavigationService.Content = new PersonPage();
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            shak.Visibility = shak.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
            dengl.Visibility = dengl.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
        }

    }
}
